package com.server.server_a10.controller;

import com.server.server_a10.entity.Home;
import com.server.server_a10.entity.Inform;
import com.server.server_a10.mapper.HomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController

public class HomeHandle {
    @Autowired
    HomeMapper homeMapper;

    //通过用户idh主房子
    @PostMapping(value = "/getUserMainHomeByUserId")
    public Inform getUserMainHomeByUserId(@RequestBody Map<String, Object> request){
        Inform inform = new Inform();

        int id = (int) request.get("id");
        try{
            int mainHomeId = homeMapper.getMainHomeByUserId(id);
            inform.success("信息获取成功",mainHomeId);
        }catch (Exception e){
            inform.fail("信息获取失败,该用户没有主房子",null);
        }

        return inform;
    }

    /**
     * 入参用户id，回参用户名下的房子
     * @param request
     * @return 房子信息
     */
    @PostMapping(value = "/getHomeMasterByUserId")
    public Inform getHomeByUserId(@RequestBody Map<String, Object> request){
        Inform inform = new Inform();
        int id = (int) request.get("id");
        List<Home> homes = homeMapper.getHomeByUserId(id);

        if (homes.isEmpty()){
            inform.setCode(100);
            inform.setMsg("用户没有自己房子，好可怜哦");
        } else {
            inform.setCode(200);
            inform.setMsg("房子信息加载完成");
            inform.setData(homes);
        }

        return inform;
    }

    @PostMapping(value = "/getRelateHomeByUserId")
    public Inform getRelateHomeByUserId(@RequestBody Map<String, Object> request){
        Inform inform = new Inform();

        int id = (int) request.get("id");
        List<Home> homes = homeMapper.getRelateHomeByUserId(id);

        if (!homes.isEmpty()){
            inform.setCode(200);
            inform.setMsg("用户房子信息已加载完毕");
            inform.setData(homes);

        }else {
            inform.setCode(100);
            inform.setMsg("用户没有房子，好可怜哦");
        }

        return inform;
    }

}
